const api = require("../../../utils/interface.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAccountList()
  },
  change(e){
    let memberId = this.data.list[e.detail.value].id
    let openId = wx.getStorageSync('openid')
    api.setDefaultPhone({ memberId, openId}).then(res=>{
      if(res.data.status==200){
        api.authorizedLogin({ openid: openId, washCardId: 1, phone: "" }).then(res => {
          if (res.data.status == 200) {
            wx.setStorageSync('userInfo', res.data.result.data)
            wx.setStorageSync('jwt', res.data.result.data.jwt)
            wx.showModal({
              title: '提示',
              content: '切换账号成功',
              showCancel: false,
              success() {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          }
        })
      }
    })
  },

  getAccountList() {
    let user = wx.getStorageSync('openid')
    api.getMemberInfoListByOpenId({ openId: user }).then(res => {
      res.data.result.map(item=>{
        if(item.phone){
          item.phone = item.phone.substr(0, 3) + "****" + item.phone.substr(7);
        }
      })
      this.setData({
        list: res.data.result
      })
    })
  }
})